﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using CE.Security;
using CE.User;
using CE.Database;
using System.Data;
using System.Collections;
public partial class MasterHome : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.HttpEquiv = "content-language";
        language.Content = Resources.Master.PageContent;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);

        HtmlLink css = new HtmlLink();
        css.Href = Resources.Master.CssUrl;
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "screen";
        Page.Header.Controls.Add(css);

        System.Web.HttpBrowserCapabilities browser = Request.Browser;

        string _strbrowser = browser.Browser;
        int _version = browser.MajorVersion;
        if (_strbrowser.ToUpper() == "INTERNETEXPLORER" || _strbrowser.ToUpper() == "IE")
        {
            if (_version <= 8)
            {
                HtmlLink cssie = new HtmlLink();
                cssie.Href = Resources.Master.CssIEUrl;
                cssie.Attributes["rel"] = "stylesheet";
                cssie.Attributes["type"] = "text/css";
                cssie.Attributes["media"] = "screen";
                Page.Header.Controls.Add(cssie);
            }
        }

        string _strIsMaintenanceActive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsMaintenanceActive");
        if (_strIsMaintenanceActive == "1")
        {
            Response.Redirect("~/maintenance/index.html");
        }
        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
        if (_strCXLive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/default");
            }
        }
        if (!IsPostBack)
        {
            string _LogoutUrl = CE.Global.General.GetUrl("/user/log-out", Page);
            HttpCookie cookies = Request.Cookies["GGQuote"];
            if (cookies != null)
            {
                if (Session[CE.Global.General.UserId] == null)
                {
                    string Email = MySecurity.Decrypt(cookies.Values.Get("CustomerEmail"), true);
                    GetUserDetails(Email);
                }
                string _strRowguid = cookies.Value;
                string _strName = MySecurity.Decrypt(cookies.Values.Get("CustomerName"), true);
                if (_strName.Length > 15)
                {
                    _strName = _strName.Substring(0, 15).ToString().Trim() + "..";
                }
                string _Details = "<span class='tx-user left'>Hi, " + _strName + "</span> <span class='left'><a href='" + _LogoutUrl + "' class='nav' style='padding-left:5px;'>Not You?</a></span>";
                lblUserSlider.Text = _Details;
                lblUserHeader.Text = _Details;
                hplNavSignInSlider.Visible = false;
                hplNavSignInHeader.Visible = false;
            }
            else
            {
                lblUserSlider.Text = "";
                lblUserHeader.Text = "";
                //hplNavSignInSlider.Visible = true;
                //hplNavSignInHeader.Visible = true;
                hplNavSignInSlider.Visible = false;
                hplNavSignInHeader.Visible = false;
            }
            hplHomeSlider.NavigateUrl = CE.Global.General.GetUrl("/", Page);
            hplHomeHeader.NavigateUrl = CE.Global.General.GetUrl("/", Page);
            hplFooterHome.NavigateUrl = CE.Global.General.GetUrl("/", Page);
            bool _isBuyComplete = false;
            HttpCookie _cookie = Request.Cookies.Get("GGQuote");
            if (_cookie != null)
            {
                Account _useraccount = new Account();
                AccountDB _useraccountdb = new AccountDB();
                _useraccount = _useraccountdb.GetUserAccount(MySecurity.Decrypt(_cookie.Values.Get("CustomerEmail"), true), Convert.ToInt32(CE.Global.CEUserType.Customer));
                if (_useraccount != null)
                {
                    Int64 CEUserId = _useraccount.CEID;
                    Int64 _result = -1;
                    MyDatabase _db = new MyDatabase();
                    DataTable _dtResult = new DataTable();
                    string _spName = "sprocCheckUserBuyingStatus";
                    SortedList _sPara = new SortedList();
                    _sPara.Add("@RefCEUserId", CEUserId);

                    _result = _db.ExecuteNonSqlSP(_spName, _sPara);
                    if (_result > 0)
                        _isBuyComplete = true;
                }
            }
            if (_isBuyComplete)
            {
                hplNavCarInsuranceSlider.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/car-details", Page);
                hplNavCarInsuranceHeader.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/car-details", Page);
            }
            else
            {
                string _randomUrl = GetCarQuoteUrl();
                hplNavCarInsuranceSlider.NavigateUrl = CE.Global.General.GetUrl(_randomUrl, Page);
                hplNavCarInsuranceHeader.NavigateUrl = CE.Global.General.GetUrl(_randomUrl, Page);
            }
            //hplNavCarInsuranceSlider.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/car-details", Page);
            hplNavFleetSlider.NavigateUrl = CE.Global.General.GetUrl("/car-fleet-insurance/", Page);
            hplNavSignInSlider.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/sign-in", Page);
            //hplNavCarInsuranceHeader.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/car-details", Page);
            hplNavFleetHeader.NavigateUrl = CE.Global.General.GetUrl("/car-fleet-insurance/", Page);
            hplNavSignInHeader.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/sign-in", Page);
            hplNavPartnersSlider.NavigateUrl = CE.Global.General.GetUrl("/partners/", Page);
            hplNavPartnersHeader.NavigateUrl = CE.Global.General.GetUrl("/partners/", Page);

            hplFooterCI.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/car-details", Page);
            hplFooterPromotion.NavigateUrl = CE.Global.General.GetUrl("/promotion/car-insurance-promotion", Page);
            hplAboutUs.NavigateUrl = CE.Global.General.GetUrl("/about-us/", Page);
            hplSavings.NavigateUrl = CE.Global.General.GetUrl("/savings/", Page);
            hplFAQ.NavigateUrl = CE.Global.General.GetUrl("/faq/", Page);
            hplPrivacy.NavigateUrl = CE.Global.General.GetUrl("/privacy/", Page);
            hplTerms.NavigateUrl = CE.Global.General.GetUrl("/terms/", Page);

            if (Session["Language"] != "en-US")
            {
                hplAboutCarInsurance.NavigateUrl = CE.Global.General.GetUrl("/car-insurance-kb/", Page);
            }
            else
            {
                //hplAboutCarInsurance.Text = "Car Insurance Broker";
                hplAboutCarInsurance.Visible = false;
            }

            hplSitemap.NavigateUrl = CE.Global.General.GetUrl("/sitemap/", Page);
            hplContactUs.NavigateUrl = CE.Global.General.GetUrl("/contact-us/", Page);
            hplNews.NavigateUrl = CE.Global.General.GetUrl("/news/", Page);
            hplMedia.NavigateUrl = CE.Global.General.GetUrl("/news/media", Page);
            hplCareers.NavigateUrl = CE.Global.General.GetUrl("/careers/", Page);
            hplTestimonials.NavigateUrl = CE.Global.General.GetUrl("/customer-testimonial/testimonial", Page);
        }
    }

    private void GetUserDetails(string CustomerEmail)
    {
        try
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.GetUserAccount(CustomerEmail.ToLower(), Convert.ToInt32(CE.Global.CEUserType.Customer));
            if (_account != null)
            {
                Session[CE.Global.General.UserId] = _account.CEID;
                Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                Session[CE.Global.General.UserEmail] = _account.Email;

                Profile _profile = new Profile();
                ProfileDB _profiledb = new ProfileDB();
                Contact _contact = new Contact();
                ContactDB _contactdb = new ContactDB();

                _contact = _contactdb.GetContact(_account.CEID);
                _profile = _profiledb.GetProfile(_account.CEID);

                if (_contact != null)
                {
                    Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                }
                else
                {
                    Session[CE.Global.General.UserPhoneNo] = null;
                }
                if (_profile != null)
                {
                    if (_profile.FirstName != null && _profile.FirstName != string.Empty)
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.FirstName) + " " + Server.HtmlDecode(_profile.SurName);
                    }
                    else
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.SurName);
                    }
                    if (_profile.IsAcceptTC != null)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "0";
                        if (_profile.IsAcceptTC == true)
                        {
                            Session[CE.Global.General.IsAcceptTC] = "1";
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    public string GetCarQuoteUrlOld()
    {
        string _url = "";
        string[] urls = new string[2];
        urls[0] = "/car-insurance/car-details";
        urls[1] = "/car-insurance/car-quote";
        Random r = new Random();
        _url = urls[r.Next(0, 2)];
        return _url;
    }

    public string GetCarQuoteUrl()
    {
        #region Promotion Status
        bool _IsPromo = false;
        string _Promo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
        Int64 _intPromo = 0;
        if (!Int64.TryParse(_Promo, out _intPromo))
        {
            _intPromo = 0;
        }
        if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
        {
            if (_intPromo >= 20150413 && _intPromo <= 20150430)
            {
                _IsPromo = true;
            }
        }
        else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
        {
            if (_intPromo >= 25580413 && _intPromo <= 25580430)
            {
                _IsPromo = true;
            }
        }
        #endregion

        string _url = "";
        Int32 _result = 0;
        string[] urls = new string[2];
        urls[0] = "/car-insurance/car-quote";
        urls[1] = "/car-insurance/car-details";
        if (_IsPromo)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            string _spName = "sprocSelectRandomNumberFromCarQuote";

            _dtResult = _db.GetDataTableBySP(_spName);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                if (!Int32.TryParse(_dtResult.Rows[0]["RandomNumber"].ToString(), out _result))
                {
                    _result = 0;
                }
            }
            _url = urls[_result];
        }
        else
        {
            _url = urls[1];
        }
        return _url;
    }

}
